﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Student_Profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MembershipUser user = Membership.GetUser();
            ProfileCommon profile = (Profile as ProfileCommon).GetProfile(user.UserName);
            txtFirstName.Text = profile.FirstName;
            txtLastName.Text = profile.LastName;
            txtMobile.Text = profile.MobileNo;
        }
        
    }

    private void clearAll()
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtMobile.Text = "";
        lablError.Text = "";
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();   
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MembershipUserCollection allUsers = Membership.GetAllUsers();

        foreach (MembershipUser us in allUsers)
        {
            ProfileCommon prof = (HttpContext.Current.Profile as ProfileCommon).GetProfile(us.UserName);
            if (prof.MobileNo == txtMobile.Text && us.UserName != Membership.GetUser().UserName)
            {
                lablError.Text = "<br/>Phone Number is already assign to another user";
                return;
            }
        }
        MembershipUser user = Membership.GetUser();
        ProfileCommon profile = (Profile as ProfileCommon).GetProfile(user.UserName);
        profile.FirstName = txtFirstName.Text;
        profile.LastName = txtLastName.Text;
        profile.MobileNo = txtMobile.Text;
        profile.Save();
        clearAll();
        lablError.Text = "Profile Updated Successfully";
    }
}